﻿
//-------------------------------------------------------
// Copyright (c) 
// All rights reserved.
// 
// File Name: SingletonT.h 
// File Des: 单例模式
// File Summary: 
// Cur Version: 1.0
// Author:
// Create Data:
// History:
//         <Author>    <Time>        <Version>      <Des>
//      lzlong        2022-11-28    1.0            
//-------------------------------------------------------
#pragma once
#include <assert.h>


/// <summary>
///        单例模式模板类,特别注意:跨模块传递时,T类需要重新实现static T& getSingleton()及static T* getSingletonPtr()
/// </summary>
template <typename T> 
class SingletonT
{
public:
    static T* ms_Singleton;

public:
    SingletonT(void)
    {
        assert(!ms_Singleton);
        ms_Singleton = static_cast<T*>(this);
    }
    virtual ~SingletonT(void)
    {
        assert( ms_Singleton );
        ms_Singleton = 0;
    }

    /// <summary>
    ///        获取单例模式的对象
    /// </summary>
    /// <returns>单例模式的对象</returns>
    static T& getSingleton()
    {
        assert(ms_Singleton);
        return (*ms_Singleton);
    }

    /// <summary>
    ///        获取单例模式的对象指针
    /// </summary>
    /// <returns>单例模式的对象指针</returns>
    static T* getSingletonPtr()
    {
        return (ms_Singleton);
    }

private:
    SingletonT& operator=(const SingletonT&)
    {
        return *this;
    }

    SingletonT(const SingletonT&) {}
};

template<class T>
typename T* SingletonT<T>::ms_Singleton = 0;
